#include <iostream>
#include <vector>


using namespace std;


class Solution {
public:

	int myAtoi(string str) {
		string rs;
		int negFlag = 0;
		int posFlag = 0;
		int numFlag = 0;

		int zeroIdx = 0;
		while (zeroIdx < str.size() && str[zeroIdx] == ' ') zeroIdx++;
		str = str.substr(zeroIdx);

		for (int i = 0; i < str.size(); i++) {
			if ((posFlag == 1 || negFlag == 1) && (str[i] == '-' || str[i] == '+')) break;
			if (numFlag == 1 && str[i] == '-') break;
			if (0 == posFlag && str[i] == '+') {
				posFlag = 1; continue;
			}
			if (0 == negFlag && str[i] == '-') {
				negFlag = 1;
				continue;
			}

			if (str[i] >= '0' && str[i] <= '9') {
				rs += str[i]; numFlag = 1;
			}
			else break;
		}
		if (rs.size() == 0) return 0;

		zeroIdx = 0;
		while (zeroIdx < rs.size() && rs[zeroIdx] == '0') zeroIdx++;
		rs = rs.substr(zeroIdx);

		if (rs.size() > 10 && negFlag) return INT_MIN;
		else if (rs.size() > 10 && 0 == negFlag) return INT_MAX;

		long long ans = 0;
		for (int i = 0; i < rs.size(); i++)
		{
			ans += rs[i] - '0';
			ans *= 10;
		}
		ans /= 10;
		if (negFlag) ans = -ans;

		if (ans > INT_MAX) return INT_MAX;
		if (ans < INT_MIN) return INT_MIN;

		return ans;
	}


};


//int main() {
//	Solution s;
//	s.myAtoi("-123.90");
//
//}